//---------------------------------------------------------------------
// File: ReceiveLocationEnabledStep.cs
// 
// Summary: 
//
//---------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//---------------------------------------------------------------------

namespace Microsoft.Services.BizTalkApplicationFramework.BizUnit
{
	using System;
	using System.Xml;
	using System.Management;
	using Microsoft.Services.BizTalkApplicationFramework.BizUnit;

	/// <summary>
	/// The ReceiveLocationEnabledStep test step checks to determine whether a specific receive location is enabled or disabled.
	/// </summary>
	/// 
	/// <remarks>
	/// The following shows an example of the Xml representation of this test step.
	/// 
	/// <code escaped="true">
    ///	<TestStep assemblyPath="" typeName="Microsoft.Services.BizTalkApplicationFramework.BizUnit.ReceiveLocationEnabledStep, Microsoft.Services.BizTalkApplicationFramework.BizUnit.BizTalkSteps">
	///		<ReceiveLocationName>GovenorIn</ReceiveLocationName>
	///		<IsDisabled>true</IsDisabled>
	///	</TestStep>
	///	</code>
	///	
	///	<list type="table">
	///		<listheader>
	///			<term>Tag</term>
	///			<description>Description</description>
	///		</listheader>
	///		<item>
	///			<term>ReceiveLocationName</term>
	///			<description>The name of the receive location to check</description>
	///		</item>
	///		<item>
	///			<term>IsDisabled</term>
	///			<description>If true is specified then the test step will check to see that the receive location is disabled, if false, the step will check it is enabled</description>
	///		</item>
	///	</list>
	///	</remarks>
	
	public class ReceiveLocationEnabledStep : ITestStep
	{
		/// <summary>
		/// ITestStep.Execute() implementation
		/// </summary>
		/// <param name='testConfig'>The Xml fragment containing the configuration for this test step</param>
		/// <param name='context'>The context for the test, this holds state that is passed beteen tests</param>
		public void Execute(XmlNode testConfig, Context context)
		{
			string receiveLocationName = context.ReadConfigAsString( testConfig, "ReceiveLocationName" );
			bool isCompareDisabled = context.ReadConfigAsBool( testConfig, "IsDisabled" );

			ManagementObject mo = GetreceiveLocationWmiObject(receiveLocationName);

			bool isActualDisabled;

			using(mo)
			{
				isActualDisabled = (bool)mo.GetPropertyValue("IsDisabled");		
			}

			if ( isCompareDisabled != isActualDisabled )
			{
				throw new ApplicationException(string.Format("The receive location: {0} was found to not be in the state IsDisabled = {1}", receiveLocationName, isCompareDisabled));
			}
			else
			{
				context.LogInfo(string.Format("The receive location: {0} was found to be in the state IsDisabled = {1}", receiveLocationName, isCompareDisabled));
			}
		}

		private static ManagementObject GetreceiveLocationWmiObject(string receiveLocation)
		{
			ManagementObjectSearcher searcher = new ManagementObjectSearcher();
			ManagementScope scope = new ManagementScope("root\\MicrosoftBizTalkServer");
			searcher.Scope = scope;
		
			// Build a Query to enumerate the MSBTS_ReceiveLocation instances if an argument
			// is supplied use it to select only the matching RL.
			SelectQuery query = new SelectQuery(); 
			query.QueryString=String.Format( "SELECT * FROM MSBTS_ReceiveLocation WHERE Name =\"{0}\"", receiveLocation );

			// Set the query for the searcher.
			searcher.Query = query;

			// Execute the query and determine if any results were obtained.
			ManagementObjectCollection queryCol = searcher.Get();
			
			ManagementObjectCollection.ManagementObjectEnumerator me = queryCol.GetEnumerator();
			me.Reset();
			if ( me.MoveNext() )
			{
				return (ManagementObject)me.Current;
			}
			else
			{
				throw new ApplicationException(string.Format("The WMI object for the receive location:{0} could not be retrieved.", receiveLocation ));
			}
		}
	}
}
